package com.smartlifev30.mvp.presenter;


import android.content.Context;
import android.net.ConnectivityManager;
import android.text.TextUtils;
import android.util.Log;

import com.smartlifev30.Constants;
import com.smartlifev30.R;
import com.smartlifev30.baiwei.App;
import com.smartlifev30.baiwei.Config;
import com.smartlifev30.baiwei.activity.LoginActivity;
import com.smartlifev30.mvp.module.ILoginModule;
import com.smartlifev30.mvp.view.ILoginView;
import com.smartlifev30.network.BaseCallBack;
import com.smartlifev30.utils.NetUtils;

import smartlifev30.com.sdk.StatusCode;
import smartlifev30.com.sdk.api.CallBack;
import smartlifev30.com.sdk.bean.response.LoginResponse;

/**
 * @author tys
 * @date 2018/8/14
 */

public class LoginPresenter extends BasePresenter<ILoginView, ILoginModule> {
    public LoginPresenter(ILoginView view, ILoginModule model) {
        super(view, model);
    }


    /**
     * 登陆服务器
     *
     * @param userName 目前全是手机号
     * @param pwd
     */
    public void loginServer(String userName, String pwd) {
        ILoginView iLoginView = view.get();
        iLoginView.showLoading();
        String user = userName.trim();
        String p = pwd.trim();
        if (TextUtils.isEmpty(user) || TextUtils.isEmpty(p)) {
            iLoginView.dismissLoading();
            iLoginView.toast(R.string.name_pwd_empty);
            return;
        }

        if (checkUserName(user)) {
            iLoginView.showLoading();
            model.loginServer(user, p, new BaseCallBack<LoginResponse>() {
                @Override
                protected void onSuccess(LoginResponse response) {
                    model.saveLoginInfo(user, p, response);
                    iLoginView.dismissLoading();
                    iLoginView.toGatewayActivity("正常登录", null);

                }

                @Override
                protected void onFailed(Throwable throwable, int code) {
                    super.onFailed(throwable, code);
                    iLoginView.dismissLoading();
                }

                @Override
                protected void onTimeOut(String detailMessage) {
                    super.onTimeOut(detailMessage);
                    iLoginView.dismissLoading();
                    int currentNetStatus = NetUtils.getCurrentNetStatus(App.getInstance());
                    if (ConnectivityManager.TYPE_WIFI == currentNetStatus) {
                        //wifi 局域网模式
                        iLoginView.toast(R.string.remote_login_failed);
                        iLoginView.toGatewayActivity("wifi登录超时", Constants.LOGIN_TIME_OUT);
                    } else {
                        super.onTimeOut(detailMessage);
                    }
                }
            });
        } else {
            iLoginView.dismissLoading();
            iLoginView.toast(R.string.name_fmort_error);
        }
    }

    /**
     * 账号格式检查
     *
     * @param userName
     */
    private boolean checkUserName(String userName) {
        return userName.length() == 11;
    }

    /**
     * 获取上次成功登录的用户名
     *
     * @param context
     * @return
     */
    public String getUserName(Context context) {
        return model.getUserName(context);
    }

    /**
     * 获取上次成功登录的密码
     *
     * @param context
     * @return
     */
    public String getPassword(Context context) {
        return model.getPassword(context);
    }
}
